package com.intelligentmeterreadingsys.checkwork.service;

import java.util.Timer;
import java.util.TimerTask;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

public class LBSService extends Service implements BDLocationListener{

	public LocationClient client;//百度定位服务类
	
	public Timer MyTime;//定时器
	
	public TimerTask MyTimeTask;//任务
	
	public boolean location_OK=false;//定位成功标志
	
	public long time;
	
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		
		if(client!=null){
			client=new LocationClient(this);
			
		}
		initLocation();
		client.start();
		initTimer();
		time=System.currentTimeMillis();
		super.onCreate();
		
	}

	@Override
	public void onDestroy() {
		if(client!=null&&client.isStarted()){
			client.stop();
		}
		super.onDestroy();
	}
	
	/**
	 * 初始化定位服务类
	 */
	public void initLocation(){
		client=new LocationClient(this);
		 LocationClientOption option = new LocationClientOption();
	        //打开GPS
	        option.setOpenGps(true);
	        //设置坐标类型
	        option.setCoorType("bd09ll");
	        
	        //禁用缓存定位
	        option.disableCache(true);
	        
	        //设置网络优先
          option.setPriority(LocationClientOption.GpsFirst);	
	        //option.setPriority(LocationClientOption.NetWorkFirst);
	        
	        //设置产品线名称
	        option.setProdName("waterSys");
	        
	        //设置定位定时时间
	        option.setScanSpan(5000);	
	        client.setLocOption(option);
	        client.registerLocationListener(this);
	        
	        
	}
    
	/**
	 * 初始化定时器
	 */
	public void initTimer(){
		if(MyTime==null){
			MyTime=new Timer();
		}
		if(MyTimeTask==null){
			MyTimeTask=new MyTimerTask();
		}
	}
	
	public void startTimer(){
		MyTime.schedule(MyTimeTask, 5000, 10000);
	}
	/**
	 * 定时任务
	 * @author king
	 *
	 */
	public class MyTimerTask extends TimerTask{

		@Override
		public void run() {}
		
	}
	@Override
	public void onReceiveLocation(BDLocation location) {
	
		   System.out.println(location.getLocType());
		   
			if(System.currentTimeMillis()-time>10000){
				Message msg=MyHandler.obtainMessage();
				msg.what=2;
				MyHandler.sendMessage(msg);
			} else if (location.getLocType()==167) {
				Message msg=MyHandler.obtainMessage();
				msg.what=1;
				MyHandler.sendMessage(msg);
									
			}
			else {
				Message msg=MyHandler.obtainMessage();
				msg.what=0;
				MyHandler.sendMessage(msg);
			}
			
			
		
		
	
		
	}

	@Override
	public void onReceivePoi(BDLocation arg0) {
		// TODO Auto-generated method stub
		
	}
   
	Handler MyHandler=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			System.out.println("--------");
			if(msg.what==1){
				System.out.println("定位失败");
				
			}else if(msg.what==0){
				System.out.println("定位成功");
			}else if(msg.what==2){
				System.out.println("定位超时");
			}
			super.handleMessage(msg);
		}
		
	};
}
